package com.example.xjtuse_pc.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

/**
 * Created by XJTUSE-PC on 2016/5/17.
 */
public class SystemInfoUtils {
    private Context context;
    private ActivityManager am;

    public  SystemInfoUtils(Context context) {
        this.context = context;
        am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    }

    /**
     * 获取正在运行的进程数
     *
     * @return
     */
    public int getRunningProcessCount() {
        //PackeManger 包管理器, 所有手机app信息
        //ActivityManager进程管理器 显示手机中运行的app信息

        java.util.List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
        return list.size();
    }


    /**
     * @return 可用内存大小
     */
    public long getAvilMem() {
        ActivityManager.MemoryInfo outinfo = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(outinfo);
        return outinfo.availMem;
    }

    /**
     *
     * @return 总内存  4.0以上用
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public long getTotalMem() {
        ActivityManager.MemoryInfo outinfo = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(outinfo);
        return outinfo.totalMem;
    }

    /**
     *  4.0以下用
     * @return 总内存
     */
    public long getLowTotalMem(){
     try {
            File file=new File("/proc/meminfo");
            FileInputStream fis=new FileInputStream(file);
            BufferedReader br=new BufferedReader(new InputStreamReader(fis));
            String line=br.readLine();
            StringBuilder sb=new StringBuilder();
            for(char c:line.toCharArray()){
                if(c>='0'&&c<='9'){
                    sb.append(c);
                }
            }
            return Long.parseLong(sb.toString())*1024;

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

}
